package cn.itcast.util;

import cn.itcast.config.QuotConfig;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

//集群连接对象
public class RedisUtil {
    /**
     * 1.新建获取连接的方法
     * 2.初始化连接池
     * 3.设置连接集群地址
     * 4.获取客户端连接对象
     */
    //TODO 1.新建获取连接的方法
    public static JedisCluster getJedisCluster(){
        //TODO 2.初始化连接池
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        int maxTotal = Integer.parseInt(QuotConfig.config.getProperty("redis.maxTotal"));
        int minIdle = Integer.parseInt(QuotConfig.config.getProperty("redis.minIdle"));
        int maxIdle = Integer.parseInt(QuotConfig.config.getProperty("redis.maxIdle"));
        jedisPoolConfig.setMaxTotal(maxTotal);
        jedisPoolConfig.setMaxTotal(minIdle);



        //TODO 3.设置连接集群地址
        //redis.host=node01:7001,node01:7002,node01:7003
        String redisCluster = QuotConfig.config.getProperty("redis.host");
        Set<HostAndPort> set = new HashSet<>();

        String[] hpArr = redisCluster.split(",");
        for (String hp : hpArr) {
            String[] h_p = hp.split(":");
            HostAndPort hostAndPort = new HostAndPort(h_p[0], Integer.parseInt(h_p[1]));
            set.add(hostAndPort);
        }

        //TODO 4.获取客户端连接对象
        JedisCluster jedisCluster = new JedisCluster(set);
        return jedisCluster;
    }

    /*public static void main(String[] args) throws IOException {
        JedisCluster jedisCluster = getJedisCluster();
        //jedisCluster.hset("product","apple","10");
        //jedisCluster.hset("product","rice","6");
        //jedisCluster.hset("product","flour","6");
        //jedisCluster.hset("product","banana","8");
        //jedisCluster.hset("product","mask","5");

        //添加预警阀值
        jedisCluster.hset("quot", "amplitude", "1") ;//振幅
        jedisCluster.hset("quot", "upDown1", "-1"); //涨跌幅-跌幅
        jedisCluster.hset("quot", "upDown2", "0.4") ;//涨跌幅-涨幅
        jedisCluster.hset("quot", "turnoverRate", "-1") ;//换手率
        jedisCluster.close();
    }*/



}
